﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using HappyDining.HPDNServiceReference;
using System.Threading.Tasks;
using HappyDining.Views;
using System.Reflection;

// The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace HappyDining
{
    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    public sealed partial class MainPage : HappyDining.Common.LayoutAwarePage
    {
       
        public MainPage()
        {
            InitializeComponent();

            init();


            // Test
            Table table = new Table();
            table.Id = 1;
            table.TableName = "ABC";
            var properties = typeof(Table).GetRuntimeProperties();
            foreach (PropertyInfo pi in properties) 
            {
                Debug.WriteLine(pi.Name + "::" + pi.GetValue(table));
                
            }
        }

        private async Task init()
        {

            try
            {
                // Gọi service để lấy ra các bàn
                // TODO:
                // Handle Exception cho trường hợp không kết nối được đến service
                HPDNServiceReference.Service1Client client = new Service1Client();
                List<Table> tables = await client.GetAllTablesAsync();

               
                client.CloseAsync();
                itemGridView.ItemsSource = tables;
            }
            catch (Exception ex)
            {
                MessageDialog dl = new MessageDialog("Không kết nối được với service.");
                dl.ShowAsync();
                Debug.WriteLine("Lỗi khởi tạo: " +ex.Message +"\n" +ex.StackTrace);
            }



        }

       
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"]
        }

        private async void itemGridView_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            /*
            int id = ((Table)e.ClickedItem).Id;
            MessageDialog dl = new MessageDialog("You clicked on: " + id);
            Debug.WriteLine("You clicked on: " + id);
            //dl.ShowAsync();
            */
            // Navigate to the detail page to show the menu detail of the table

            Table table = (Table)e.ClickedItem;
            if (table.Status == 0) // Free
            {
                MessageDialog md = new MessageDialog("" +table.TableName+" đang trống");
                md.Title = table.TableName;
                await md.ShowAsync();
            }
            else    //Table is busy
            {
                this.Frame.Navigate(typeof(ChiTietHoaDon), table);
            }
            
        }

        private async void monAnAppBarButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            this.TopAppBar.IsOpen = false;
            /*
             
            MessageDialog md = new MessageDialog("Phiên bản miễn phí không có chức năng này.\nVui lòng nâng cấp lên phiên bản trả phí !!!");
            md.Title = "THÔNG BÁO";
            await md.ShowAsync();
             */

            this.Frame.Navigate(typeof(QuanLyMon));
        }

        private void exitButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            
        }

        
    }
}
